﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

public partial class Home : System.Web.UI.Page
{
    private string strconn;
    SqlCommand cmd;
    SqlConnection conn;
    DataSet ds;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        txtpass.Attributes["value"] = txtpass.Text;
        strconn = WebConfigurationManager.ConnectionStrings["photoconnection"].ToString();
        conn = new SqlConnection(strconn);
        ds = new DataSet();
        if (!IsPostBack)
            load();
    }

    protected void load()
    {
        try
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd = new SqlCommand("pdselect", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void btnlogin_Click(object sender, EventArgs e)
    {
        Application["userid"] = txtuser.Text;
        if (Application["guestid"] != null)
        {
            Application.Lock();
            Application["guestid"] = Convert.ToInt32(Application["guestid"]) + 1;
            Application.UnLock();
        }
        else
        {
            Application["guestid"] = 1;
        }
        Response.Cookies["login"]["userid"] = txtuser.Text;
        Response.Cookies["login"]["pass"] = txtpass.Text;
        Response.Cookies["login"]["guestid"] = Application["guestid"].ToString();
        Response.Cookies["login"].Expires = DateTime.MaxValue;
        try
        {
            
           

            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd = new SqlCommand("userlogin", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@user", SqlDbType.VarChar).Value = txtuser.Text;
            cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = txtpass.Text;
            //lbl1.Text = "success";

            object i = cmd.ExecuteScalar();
            if (i != null)
            {
                if (Request.Cookies["login"]["userid"] == txtuser.Text && Request.Cookies["login"]["pass"] == txtpass.Text)
                {
                    Application["userid"] = txtuser.Text;
                    Application["guestid"] = Request.Cookies["login"]["guestno"];
                    lbluser1.Visible = false;
                    lblpass.Visible = false;
                    txtuser.Visible = false;
                    txtpass.Visible = false;
                    btnlogin.Visible = false;
                    lblwelcome.Visible = true;
                    lbstatus.Visible = true;
                    lbstatus.Text = Application["userid"].ToString();
                }
                
              
            }
            else
            {
                lbstatus.Visible = true;
                lbstatus.Text = "false";//SAI
            }
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    }
